<?php
// $Id: waimai.php 257 2012-04-12 12:56:59Z tomqin@gmail.com $

class WaimaiAction extends Action{

    public $params = array();

    function beforeAction(){
        Core::import('system.service.include.CityZone');
        $this->zone = new CityZone;
    }

    function onDefault(){
        $orders = $this->getNewestOrder();

        $big_zone_id = $sub_zone_id = 0;
        $currZone = $this->zone->getZoneByPy($this->method);
        if($currZone){
            if($currZone->parent_id == 0){
                $big_zone_id = $currZone->zone_id;
                $sub_zone_id = 0;
            }else{
                $big_zone_id = $currZone->parent_id;
                $sub_zone_id = $currZone->zone_id;
            }
        }

        $perpage = 10;
        $params = $this->parseParam();

        $query  = str_replace(array('"',"%","&","'"), '', Arr::getString($_REQUEST, 'q'));
        $page   = $_REQUEST['page'] = $params['p'];
        $result = $this->search($big_zone_id, $sub_zone_id, $page, $perpage, $query);
        $total  = $result[0]['total'];
p($result);
        $urltpl = '/'. $this->action .'/'. $this->method . '/p{PAGE}?q='.urlencode($query);
        $pages  = View::pages($total, $page, $perpage, $urltpl);

        $bigzone  = $this->zone->getZoneById($big_zone_id);
        $subzone  = $this->zone->getZoneById($sub_zone_id);
        $bigzone  = $bigzone ? $bigzone->name : '';
        $subzone  = $subzone ? $subzone->name : '';

        $title = "{CITY}" .($query?$query:$subzone). "外卖信息_{CITY}{$bigzone}外卖送餐_网上订餐";
        $keywords = '{CITY}外卖订餐美食网';
        $description = '速订客—{CITY}外卖网，{CITY}订餐，{CITY}美食。{CITY}最好的网上订餐平台，为您提供最新最全的24小时外卖送餐电话、外卖菜单等外卖订餐详细信息。包括肯德基网上订餐电话，麦当劳网上订餐电话。';
        $this->seoContent($title, $keywords, $description);

        $this->setVar('zone', $this->zone);
        $this->setVar('result', $result);
        $this->setVar('query', $query);
        $this->setVar('pages', $pages);
        $this->setVar('big_zone_id', $big_zone_id);
        $this->setVar('sub_zone_id', $sub_zone_id);
        $this->setVar('orders', $orders);
        $this->render('waimai-search');

        View::dump();
    }

    function search($big_zone_id, $sub_zone_id, $page, $perpage = 5, $query = ''){
        $big_zone_id = intval($big_zone_id);
        $sub_zone_id = intval($sub_zone_id);
        $page        = intval($page);
        $perpage     = intval($perpage);
        $cat_id      = 0;

        $result = Db::proc("call sp_search_waimai({$perpage}, {$page}, {$this->cityId}, {$big_zone_id}, {$sub_zone_id}, '{$cat_id}', '{$query}')");
        return $result[0];
    }

    private function parseParam(){
        $param = explode('-', $this->params);
        $array = array();
        foreach($param as $var){
            $array[substr($var, 0, 1)] = substr($var, 1);
        }
        $array['p'] = Arr::getInt($array, 'p', 1);
        return $array;
    }

    private function getNewestOrder(){
        return Db::getAll("SELECT s.shop_id, s.name, o.realname, o.create_dt FROM `biz_order` o, `biz_shop` s WHERE o.shop_id = s.shop_id Order by o.create_dt DESC LIMIT 10");
    }
}

?>